package com.imooc.common.handler;

import com.imooc.common.config.ProjectUrlConfig;
import com.imooc.common.exception.SellerAuthException;
import com.imooc.common.exception.SelllException;
import com.imooc.common.utils.ResultVoUtil;
import com.imooc.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class MyExceptionHandler {

    @Autowired
    ProjectUrlConfig config;

    @ExceptionHandler(value = SellerAuthException.class)
    public ModelAndView handlerAuthException() {
        return new ModelAndView("redirect:"
                .concat(config.getWechatOpenAuthorize())
                .concat("/sell/wechat/qrAuthorize")
                .concat("?returnUrl=")
                .concat(config.getSell())
                .concat("/sell/seller/login"));
    }

    @ExceptionHandler(value = SelllException.class)
    @ResponseBody
    public ResultVo handlerSellException(SelllException exception) {
        return ResultVoUtil.error(exception.getCode(), exception.getMessage());
    }

}
